

package lc3vm;


public class Convertidor {


    //Convertir de hexadecimal a binario
    public static String hexBin(String hex) {
        String bin = "";
        char caracter;
        
        hex = hex.replaceAll("x", "");


        for(int i=0; i < hex.length(); i++) {
            caracter = hex.charAt(i);

            
            switch(caracter) {
                
                case '0':
                    bin = bin + "0000";
                break;

                case '1':
                    bin = bin + "0001";
                break;

                case '2':
                    bin = bin + "0010";
                break;

                case '3':
                    bin = bin + "0011";
                break;

                case '4':
                    bin = bin + "0100";
                break;

                case '5':
                    bin = bin + "0101";
                break;

                case '6':
                    bin = bin + "0110";
                break;

                case '7':
                    bin = bin + "0111";
                break;

                case '8':
                    bin = bin + "1000";
                break;

                case '9':
                    bin = bin + "1001";
                break;

                case 'A':
                    bin = bin + "1010";
                break;

                case 'B':
                    bin = bin + "1011";
                break;

                case 'C':
                    bin = bin + "1100";
                break;

                case 'D':
                    bin = bin + "1101";
                break;

                case 'E':
                    bin = bin + "1110";
                break;

                case 'F':
                    bin = bin + "1111";
                break;

                default:
                    System.out.println("Error conversion hexBin");
                break;
            }


        }
        
        return bin;

    }

    public static String hexBin8(String hex){
        int dec = hexInt(hex);
        return decBin8(dec);
    }

    public static String decBin9(int dec){
        String s = Integer.toBinaryString(dec);

        if(s.length() < 9)
            for(int i = s.length(); i < 10; i++)
                s = "0" + s;
        if(s.length() > 9){
            s = s.substring(s.length()-9);

        }
        return(s);
    }

    public static String decBin5(int dec){
        String s = Integer.toBinaryString(dec);

        if(s.length() < 5)
            for(int i = s.length(); i < 6; i++)
                s = "0" + s;
        if(s.length() > 5){
            s = s.substring(s.length()-5);

        }
        return(s);
    }

    public static String decBin8(int dec){
        String s = Integer.toBinaryString(dec);

        if(s.length() < 8)
            for(int i = s.length(); i < 9; i++)
                s = "0" + s;
        if(s.length() > 8){
            s = s.substring(s.length()-8);

        }
        return(s);
    }

    public static String decHex(int dec){
       String numeroHex;

       numeroHex = Integer.toHexString(dec);
       numeroHex = numeroHex.toUpperCase();
       if(numeroHex.length() < 4)
           for(int i = numeroHex.length(); i < 4; i++)
               numeroHex = "0" + numeroHex;
       numeroHex = "x" + numeroHex;

       return(numeroHex);
    }

    public static String binHex(String bin){

        int numeroDecimal;
        String numeroHex;

        numeroDecimal = binInt(bin);
        numeroHex = Integer.toHexString(numeroDecimal);
        numeroHex = numeroHex.toUpperCase();
        if(numeroHex.length() < 4)
            for(int i = numeroHex.length(); i < 4; i++)
                numeroHex = "0" + numeroHex;
        numeroHex = "x" + numeroHex;

        return(numeroHex);
    }

    public static int hexInt(String hex) {

        String bin;
        int decimal;

        bin = hexBin(hex);
        decimal = binInt(bin);

        return decimal;
    }

    // Convertir de hexadecimal a decimal
   /* public static int hexInt(String hex){

        int decimal = 0, j = hex.length()-1;;
        String caracterHex; //como char  no se puede convertir en integer se usa este string temporal
        Double elevado; //variable temporal que sirve para elevar y dsps convertirlo a int


        //Se quita la x que indica que es hexadecimal
        hex.replaceAll("x", "");

        //Se hace la conversion
        for(int i = 0; i < hex.length(); i++){
            caracterHex = "" + hex.charAt(i);
            elevado = Math.pow(16, (double)i);
            if((hex.charAt(i) <= '9') && (hex.charAt(j) >= '0'))
                decimal = decimal + (Integer.parseInt(caracterHex) * elevado.intValue());
            else{
                switch(hex.charAt(i)){
                    case 'A': decimal = decimal + (10 * elevado.intValue());
                    break;
                    case 'B': decimal = decimal + (11 * elevado.intValue());
                    break;
                    case 'C': decimal = decimal + (12 * elevado.intValue());
                    break;
                    case 'D': decimal = decimal + (13 * elevado.intValue());
                    break;
                    case 'E': decimal = decimal + (14 * elevado.intValue());
                    break;
                    case 'F': decimal = decimal + (15 * elevado.intValue());
                    break;
                    case 'a': decimal = decimal + (10 * elevado.intValue());
                    break;
                    case 'b': decimal = decimal + (11 * elevado.intValue());
                    break;
                    case 'c': decimal = decimal + (12 * elevado.intValue());
                    break;
                    case 'd': decimal = decimal + (13 * elevado.intValue());
                    break;
                    case 'e': decimal = decimal + (14 * elevado.intValue());
                    break;
                    case 'f': decimal = decimal + (15 * elevado.intValue());
                    break;
                    default: System.out.println("No es hexadecimal");
                }
            }
            j--;
        }

        return(decimal);
    }*/

    public static int binInt (String bin){

        int decimal = 0, j = bin.length()-1;
        String caracterBin;
        Double elevado;

        bin.replaceAll("b", "");
        for (int i = 0 ; i < bin.length(); i++){
            caracterBin = "" + bin.charAt(j);
            elevado = Math.pow(2, (double) i);
            if ((bin.charAt(j) == '0') || (bin.charAt(j) == '1'))
                decimal = decimal + (Integer.parseInt(caracterBin) * elevado.intValue());
            else
                System.out.println("No es binario");
            j--;
        }
        return(decimal);
    }

    public static int binC2Int (String bin){

        int decimal = binInt(bin);
        
        if(bin.charAt(0) == '1') {
            decimal = decimal - 1;
            decimal = ~decimal;
            decimal = decimal * -1;
        }

        return decimal;
    }

    public static int decInt (String dec) {

        dec = dec.replaceAll("#", "");

        return Integer.parseInt(dec);
    }

    /*
    public static int binC2Int (String bin){

        int decimal = 0, j = bin.length()-1;
        String caracterBin;
        Double elevado;

        bin.replaceAll("b", "");
        for (int i = 0 ; i < bin.length(); i++){
            caracterBin = "" + bin.charAt(j);
            elevado = Math.pow(2, (double) i);
            if ((bin.charAt(j) == '0') || (bin.charAt(j) == '1'))
                if (j == 0)
                    decimal = decimal + (Integer.parseInt(caracterBin) * elevado.intValue()*-1);
                else
                    decimal = decimal + (Integer.parseInt(caracterBin) * elevado.intValue());
            else
                System.out.println("No es binario");
            j--;
        }
        return(decimal);
    }
    */
}